# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver

# Create your models here.
class Profile(models.Model):
    user = models.OneToOneField(User,on_delete=models.CASCADE)
    resume = models.TextField(max_length=500,blank=True)
    location = models.CharField(max_length=30,blank=True)
    birth_date = models.DateField(null=True,blank=True)


    def __unicode__(self):
        return self.user.username

@receiver(post_save,sender=User)
def create_user_profiole(sender,instance,created,**kwargs):
    if created:
        Profile.objects.create(user=instance)

@receiver(post_save,sender=User)
def save_user_profile(sender,instance,**kwargs):
    print "save"
    instance.profile.save()


class CommonUser(models.Model):
    username = models.CharField(max_length=100)
    password = models.CharField(max_length=100)
    email = models.CharField(max_length=100)
    resume = models.TextField(max_length=500, blank=True)
    location = models.CharField(max_length=30, blank=True)
    birth_date = models.DateField(null=True, blank=True)
    def __unicode__(self):
        return self.username


class Written(models.Model):
    profile_id = models.ForeignKey(CommonUser,on_delete=models.CASCADE)
    title = models.CharField(max_length=100)
    content = models.TextField(max_length=500,)
    submit_time = models.DateTimeField(auto_now=True)
    def __unicode__(self):
        return self.title